package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.Goods;
import com.rpframework.website.device.domain.TCollect;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.CollectService;
import com.rpframework.website.device.service.GoodsService;
import com.rpframework.website.device.util.HConstants;
@Controller
@RequestMapping("api/collect")
public class ApiCollectAct extends AdminAct{
	@Resource CollectService service;	
	@Resource GoodsService goodsService;	
	
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager",required = false ) Pager<TCollect> pager,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<TCollect>();
		}
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		pager.getSearchMap().put("userId",sessionuser.getId().toString());
		pager = service.getPager(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<TCollect> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(TCollect t : list){
				JsonObject obj = new JsonObject();
				Goods g = goodsService.select(t.getGoodsId());
				if(g!=null && StringUtils.isNotBlank(g.getUrlList())){
					JsonArray arr = new JsonParser().parse(g.getUrlList()).getAsJsonArray();
					obj.add("urlList", arr);//商品图片
					obj.addProperty("goodsName", g.getName());
					obj.addProperty("id", t.getId());	
					obj.addProperty("userId", t.getUserId());	
					obj.addProperty("goodsId", g.getId());	
					array.add(obj);
				}
			}
			json.add("arrays", array);
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
	@RequestMapping(value="/add",produces = "application/json; charset=utf-8")
	public @ResponseBody String add(
			@RequestParam(value = "goodsId") Integer goodsId,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		JsonObject json = new JsonObject();
		/*查出当前用户对这件商品有没有收藏过，有收藏就取消，没收藏就收藏*/
		boolean flag  = service.collectByGoods(sessionuser.getId(),goodsId);
		json.addProperty("code", 200);
		if(flag){
			json.addProperty("msg", "收藏成功");
		}else{
			json.addProperty("msg", "取消收藏成功");
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
}
